<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>layui</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="layuiadmin/layui/css/layui.css" media="all">
</head>
<body>
	<!-- 表单编辑事件 -->
	<!-- 隐藏表单 -->
	<div id="editForm" style="display: none">
		<ins class="adsbygoogle"
			style="display: inline-block; width: 300px; height: 15px"
			data-ad-client="ca-pub-6111334333458862" data-ad-slot="3820120620"></ins>
		<!-- 	<fieldset class="layui-elem-field layui-field-title"
		style="margin-top: 50px;">
		<legend>编辑</legend>
	</fieldset> -->

		<form class="layui-form" action="" lay-filter="example">
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">用户ID</label>
					<div class="layui-input-inline">
						<input type="text" name="userid" id="userid" readonly="readonly" autocomplete="off"
							class="layui-input">
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">用户名</label>
					<div class="layui-input-inline">
						<input type="text" name="username" id="username" readonly="readonly"
							autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">密码</label>
					<div class="layui-input-inline">
						<input type="text" name="userpwd" id="userpwd" autocomplete="off"
							class="layui-input">
					</div>
				</div>

				<div class="layui-inline">
					<label class="layui-form-label">手机号</label>
					<div class="layui-input-inline">
						<input type="text" name="usertel" id="usertel" autocomplete="off"
							class="layui-input">
					</div>
				</div>

				<div class="layui-form-item">
					<label class="layui-form-label">性别</label>
					<div class="layui-input-block">
						<input type="radio" name="usersex" id="usersex" value="男"
							title="男" checked=""> <input type="radio" name="usersex"
							id="usersex" value="女" title="女">

					</div>
				</div>

				<div class="layui-form-item">
					<label class="layui-form-label">用户状态</label>
					<div class="layui-input-block">
						<input type="radio" name="userstatus" id="userstatus" value="0"
							title="白名单" checked=""> <input type="radio"
							name="userstatus" id="userstatus" value="1" title="黑名单">
					</div>
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-input-block">
					<button class="layui-btn" lay-submit="" lay-filter="demo1">提交修改</button>
				</div>
			</div>
		</form>
	</div>


	<!-- 搜索 -->
		<div class="demoTable">
		搜索名字：
		<div class="layui-inline">
			<input class="layui-input" name="id" id="demoReload"
				autocomplete="off">
		</div>
		<button class="layui-btn" data-type="reload">搜索</button>
	</div>

	<table class="layui-hide" id="test" lay-filter="test"></table>

	<script type="text/html" id="toolbarDemo">
			<div class="layui-btn-container">
				<!--<button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>-->
				<!-- <button class="layui-btn layui-btn-sm" lay-event="getCheckData">添加员工</button> -->
				<button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
				<button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
				<!-- <button class="layui-btn layui-btn-sm" lay-event="add">添加员工</button>
			</div>
		</script>

	<script type="text/html" id="barDemo">
			<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
			<!--<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>-->
		</script>

	<script src="layuiadmin/layui/layui.js"></script>
	<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

	<script>
		layui.use('table', function() {
			var table = layui.table;

			table.render({
				elem : '#test'
				//  ,url:'/test/table/demo1.json'
				,
				url : 'userapi/user'//模拟接口
				,
				toolbar : '#toolbarDemo',
				title : '用户数据表',
				cols : [ [ {
					type : 'checkbox',
					fixed : 'left'
				}, {
					field : 'userid',
					width : 100,
					title : '用户ID',
					sort : true
				}, {
					field : 'username',
					title : '姓名',
					minWidth : 100
				}, {
					field : 'userpwd',
					title : '密码'
				}, {
					field : 'usersex',
					width : 80,
					title : '性别'
				}, {
					field : 'usertel',
					title : '手机'
				}, {
					field : 'userstatus',
					title : '用户状态',
					sort : true,
					templet : function(res) {
						return res.userstatus == 0 ? "白名单" : "黑名单";
					}

				}, {
					fixed : 'right',
					title : '操作',
					toolbar : '#barDemo',
					width : 150
				} ] ],
				page : true,
				id : 'testReload'
			});

			//头工具栏事件
			table.on('toolbar(test)', function(obj) {
				var checkStatus = table.checkStatus(obj.config.id);
				switch (obj.event) {
				case 'getCheckData':
					var data = checkStatus.data;
					layer.alert(JSON.stringify(data));
					break;
				case 'getCheckLength':
					var data = checkStatus.data;
					layer.msg('选中了：' + data.length + ' 个');
					break;
				case 'isAll':
					layer.msg(checkStatus.isAll ? '全选' : '未全选');
					break;
				}
				;
			});

			//监听行工具事件
			table.on('tool(test)', function(obj) {
				var data = obj.data;
				if (obj.event === 'edit') {
					$("#userid").attr("value", data.userid);
					$("#username").attr("value", data.username);
					$("#userpwd").attr("value", data.userpwd);
					$("#usertel").attr("value", data.usertel);

					$("input[name=usersex][value=男]").attr("checked",
							data.usersex == "男" ? true : false);
					$("input[name=usersex][value=女]").attr("checked",
							data.usersex == "女" ? true : false);

					$("input[name='userstatus'][value=0]").attr("checked",
							data.userstatus == 0 ? true : false);
					$("input[name='userstatus'][value=1]").attr("checked",
							data.userstatus == 1 ? true : false);

					var index1 = layer.open({
						type : 1,
						content : $("#editForm").html(),
					});
					//只有加了这一句，表单的复选框，单选框才可以编辑
					layui.form.render();
					//监听提交
					layui.form.on('submit(demo1)', function(data2) {
						var d = data2.field;
						/* 	console.log(d);
							//JSON.stringify
							console.log(JSON.stringify(d)); */
						$.ajax({
							type : "put",
							url : "userapi/user",
							contentType : "application/json",
							data : JSON.stringify(d),
							success : function(data) {
								if (data.msg == "修改成功") {
									layer.msg(data.msg, {
										time : 1000
									}, function() {
										layer.close(index1);
									});

								} else {
									layer.msg(data.msg)
								}
							}
						});

						/* return false; */
					});
				}
			});

			var $ = layui.$, active = {
					reload : function() {
						var demoReload = $('#demoReload');

						//执行重载
						table.reload('testReload', {
							page : {
								curr : 1
							//重新从第 1 页开始
							},
							where : {
								// 		          key: {
								// 		            content: demoReload.val()
								// 		          }
								content : demoReload.val()
							}
						});
					}
				};

				$('.demoTable .layui-btn').on('click', function() {
					var type = $(this).data('type');
					active[type] ? active[type].call(this) : '';
				});
		});
	</script>

</body>
</html>